﻿using AutoMapper;
using SIG.Model.Articles;
using SIG.Service;
using SIG.ViewModel.Front.Articles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace SIG.WebMVC.Controllers.API
{
    public class ArticleController : BaseApiController
    {
        private readonly IArticleCategoryService _categoryService;
        private readonly IArticleService _articleService;
        private readonly IPageMetaService _pageMetaService;
        private readonly IMapper _mapper;

        public ArticleController(IArticleCategoryService categoryService, IArticleService articleService, IPageMetaService pageMetaService, IMapper mapper)
        {
            _categoryService = categoryService;
            _articleService = articleService;
            _pageMetaService = pageMetaService;
            _mapper = mapper;

        }

        [HttpGet]
        [Route("api/article/list")]

        public async Task<HttpResponseMessage> List()
        {
            try
            {
                var goodsList = await _articleService.GetActiveElementsAync();
                var articles = _mapper.Map<IList<Article>, IList<ArticleVM>>(goodsList);
                return Request.CreateResponse(HttpStatusCode.OK, articles);
            }
            catch (Exception ex)
            {
                HttpError err = new HttpError(ex.Message);
                return Request.CreateResponse(HttpStatusCode.BadRequest, err);
            }

        }
    }


}
